package com.ruoyi.web.controller.api;

import com.ruoyi.cat.domain.CatArticletaskTable;
import com.ruoyi.cat.domain.CatShareuserTable;
import com.ruoyi.cat.domain.CatWinningRecords;
import com.ruoyi.cat.domain.DTO.WinningDTO;
import com.ruoyi.cat.domain.DTO.WriteDTO;
import com.ruoyi.cat.domain.VO.ApiSaveShareVO;
import com.ruoyi.cat.domain.VO.ScanTheCodeVO;
import com.ruoyi.cat.service.ICatActivityTableService;
import com.ruoyi.cat.service.ICatWinningRecordsService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.WxLoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.framework.web.service.WxTokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Api(tags = "api-核销管理相关接口")
@RestController
@RequestMapping("/api/writeOff")
public class WriteOffController extends BaseController {

    @Autowired
    private ICatActivityTableService iCatActivityTableService;

    @Autowired
    private ICatWinningRecordsService iCatWinningRecordsService;

    @Autowired
    private WxTokenService wxTokenService;

    @ApiOperation("核销管理列表")
    @GetMapping("getWriteList")
    public TableDataInfo getArticleList(HttpServletRequest request) {
        WxLoginUser wxLoginUser = wxTokenService.getWxUser(request);
        startPage();
        List<WriteDTO> list =  iCatActivityTableService.getWriteList(wxLoginUser.getUserId());
        return getDataTable(list);
    }

    @ApiOperation("活动核销列表")
    @GetMapping("getVerification")
    public TableDataInfo getVerification(HttpServletRequest request,Long articleId) {
        startPage();
        List<WinningDTO> list =  iCatWinningRecordsService.getVerification(articleId);
        return getDataTable(list);
    }

    @ApiOperation("api-扫码核销")
    @PostMapping("scanTheCode")
    public AjaxResult scanTheCode(HttpServletRequest request, @Validated @RequestBody ScanTheCodeVO scanTheCodeVO) {
        CatWinningRecords catWinningRecords = iCatWinningRecordsService.selectCatWinningRecordsByRedemptionCode(scanTheCodeVO.getRedemptionCode());
        if (catWinningRecords == null) {
            return error("核销码不存在");
        }
        if (catWinningRecords.getWhether().equals("1")) {
            return error("该码已经核销过，不得重复核销");
        }
        WxLoginUser wxLoginUser = wxTokenService.getWxUser(request);
        CatWinningRecords catShareuserTable = new CatWinningRecords();
        catShareuserTable.setRedemptionCode(scanTheCodeVO.getRedemptionCode());
        catShareuserTable.setAuditId(wxLoginUser.getUserId());
        catShareuserTable.setAuditTime(new Date());
        catShareuserTable.setWhether("1");
        return success(iCatWinningRecordsService.updateCatWinningRecordsByRedemptionCode(catShareuserTable));
    }

}
